﻿using System;
using System.Collections.Generic;
using UnityEngine.UIElements;
using winS.UnityEditor.UIElement;
using Button = winS.UnityEditor.UIElement.Button;
using DropdownField = winS.UnityEditor.UIElement.DropdownField;

namespace winS.UnityEditor.GUI
{
    public class ObjectEditorControl : VisualElement
    {
        public event Action onSaved;
        public event Action<int> onDeleted;
        public event Action<int> onSelected;
        public event Action<string> onCreated;
        public event Action<bool> onEditingStateChanged;

        public int index { get => dropdownField.index; set => dropdownField.index = value; }
        public IEnumerable<string> displayNames => displayNameList;

        public Button saveButton { get; private set; }
        public Button createButton { get; private set; }
        public Button deleteButton { get; private set; }
        public CreateBar createBar { get; private set; }

        protected virtual string defaultObjectName => "对象";
        protected Row hubContainer { get; private set; }

        private readonly DropdownField dropdownField;
        private readonly VerticalSlideView verticalSlideView;
        private readonly HorizontalSlideView horizontalSlideView;
        private readonly List<string> displayNameList = new List<string>();

        public ObjectEditorControl() : this(GUIUtility.guiFactory)
        {
        }
        public ObjectEditorControl(GUIFactory guiFactory)
        {
            Add(horizontalSlideView = guiFactory.CreateHorizontalSlideView());

            Row hub = guiFactory.CreateRow();
            horizontalSlideView.Add(hub);

            hub.Add(dropdownField = guiFactory.CreateDropdownField(displayNameList, OnDropdownFieldChangedCallback));
            dropdownField.style.flexGrow = 1f;
            dropdownField.style.flexShrink = 0f;

            hub.Add(verticalSlideView = guiFactory.CreateVerticalSlideView());
            verticalSlideView.Add(hubContainer = guiFactory.CreateRow());
            hubContainer.Add(createButton = guiFactory.CreateButton(ShowCreateBar));
            hubContainer.Add(deleteButton = guiFactory.CreateButton(OnDeleteButtonClickCallback));
            hubContainer.Add(saveButton = guiFactory.CreateButton(() => onSaved?.Invoke()));

            Row deleteBar = guiFactory.CreateRow();
            deleteBar.Add(guiFactory.CreateLabel("确认删除?"), 1f);
            deleteBar.Add(guiFactory.CreateButton("确认", OnConfirmDeleteCallback));
            deleteBar.Add(guiFactory.CreateButton("取消", CancelDelete));
            verticalSlideView.Add(deleteBar);

            saveButton.SetEnabled(false);
            deleteButton.SetEnabled(false);

            horizontalSlideView.Add(createBar = guiFactory.CreateCreateBar(name =>
            {
                ShowHub();
                onCreated?.Invoke(name);
            }, ShowHub));

            SetObjectName(defaultObjectName);
        }

        public void SetIndexByName(string name)
        {
            dropdownField.index = displayNameList.IndexOf(name);
        }
        public void AddDisplayNames(IEnumerable<string> names)
        {
            int maxIndex = 0;
            foreach (string name in names)
            {
                maxIndex++;
                displayNameList.Add(name);
            }
            if (maxIndex == 0) return;
            dropdownField.Refresh();
        }
        public void ClearDisplayNames()
        {
            displayNameList.Clear();
            saveButton.SetEnabled(false);
            deleteButton.SetEnabled(false);
            dropdownField.index = -1;
        }
        public void SetObjectName(string objectName)
        {
            saveButton.SetLabel($"保存{objectName}");
            createButton.SetLabel($"创建{objectName}");
            deleteButton.SetLabel($"删除{objectName}");
            dropdownField.label.text = $"{objectName}名称";
            createBar.label = $"新{objectName}名";
        }

        protected virtual void OnIndexChanged(int index)
        {
        }

        private void ShowHub()
        {
            onEditingStateChanged?.Invoke(true);
            horizontalSlideView.SlideContent(0);
        }
        private void ShowCreateBar()
        {
            onEditingStateChanged?.Invoke(false);
            horizontalSlideView.SlideContent(1);
            createBar.Focus();
        }
        private void CancelDelete()
        {
            verticalSlideView.SlideContent(0);
            dropdownField.SetEnabled(true);
            onEditingStateChanged?.Invoke(true);
        }

        private void OnConfirmDeleteCallback()
        {
            CancelDelete();
            onDeleted?.Invoke(index);
        }
        private void OnDeleteButtonClickCallback()
        {
            onEditingStateChanged?.Invoke(false);
            verticalSlideView.SlideContent(1);
            dropdownField.SetEnabled(false);
        }
        private void OnDropdownFieldChangedCallback(string newValue)
        {
            int index = displayNameList.IndexOf(newValue);
            OnIndexChanged(index);
            if (index < 0) return;
            saveButton.SetEnabled(true);
            deleteButton.SetEnabled(true);
            onSelected?.Invoke(index);
        }
    }
}